﻿Imports System.Resources
Imports System.Drawing
Imports System.Reflection
Imports System.ComponentModel
Imports System.Windows.Forms
Imports ajayumi.develop.win32.Monster.Base
Imports ajayumi.develop.VBasic.Win.Controls.Forms

Public Class FrmLady
    Inherits ImageForm

    Private m_BackgroundWorker As BackgroundWorker = Nothing
    Private m_ActionManager As ActionManager = Nothing

    Protected Overrides Function InitFormImage() As System.Drawing.Image
        Dim _Assembly As Assembly = Reflection.Assembly.Load(My.Settings.ResName)
        Dim _ResourceManager As ResourceManager = New ResourceManager(String.Concat(My.Settings.ResName, ".Resources"), _Assembly)

        Dim _Image As Image = DirectCast(_ResourceManager.GetObject("zmg_0"), Image)
        Return _Image
    End Function

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        Dim _Action As ActionBase = Nothing
        Me.m_ActionManager = New ActionManager()
        With Me.m_ActionManager
            _Action = New NormalAction()
            AddHandler _Action.ImageChanged, AddressOf Me.Task_ImageChanged
            .Add(_Action)
            _Action = New RunLeftAction()
            AddHandler _Action.ImageChanged, AddressOf Me.Task_ImageChanged
            .Add(_Action)
            _Action = New RunRightAction()
            AddHandler _Action.ImageChanged, AddressOf Me.Task_ImageChanged
            .Add(_Action)
            _Action = New DrawHeartShaped()
            AddHandler _Action.ImageChanged, AddressOf Me.Task_ImageChanged
            .Add(_Action)
            _Action = New DisrobeAction()
            AddHandler _Action.ImageChanged, AddressOf Me.Task_ImageChanged
            .Add(_Action)
            _Action = New DressAction()
            AddHandler _Action.ImageChanged, AddressOf Me.Task_ImageChanged
            .Add(_Action)
            _Action = New SayLoveAction()
            AddHandler _Action.ImageChanged, AddressOf Me.Task_ImageChanged
            .Add(_Action)
            _Action = New BatheAction()
            AddHandler _Action.ImageChanged, AddressOf Me.Task_ImageChanged
            .Add(_Action)
            _Action = New GoodbyeAction()
            AddHandler _Action.ImageChanged, AddressOf Me.Task_ImageChanged
            .Add(_Action)
        End With

        Me.m_BackgroundWorker = New BackgroundWorker()
        With Me.m_BackgroundWorker
            .WorkerReportsProgress = True
            .WorkerSupportsCancellation = True
            AddHandler .DoWork, AddressOf Me.BackgroundWorker_DoWork
            AddHandler .ProgressChanged, AddressOf Me.BackgroundWorker_ProgressChanged
            AddHandler .RunWorkerCompleted, AddressOf Me.BackgroundWorker_RunWorkerCompleted

            .RunWorkerAsync()
        End With
    End Sub

    Protected Overrides Sub OnMouseDoubleClick(ByVal e As System.Windows.Forms.MouseEventArgs)
        Me.m_ActionManager.Exit()
    End Sub

    Private Sub Task_ImageChanged(ByVal sender As Object, ByVal e As ImageChangedEventArgs)
        Me.UpdateFormImage(e.Img)
        Me.UpdateLocation(Me.Left + e.ImgInfo.OffsetX, Me.Top + e.ImgInfo.OffsetY)
    End Sub




    Private Sub BackgroundWorker_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs)
        Me.m_ActionManager.Init(sender)
    End Sub

    Private Sub BackgroundWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs)
        Dim _TempStr As String = String.Format("{0}%  加载资源：{1}", _
                                               e.ProgressPercentage, DirectCast(e.UserState, ImageInfo).Name)
        Using _ImageClone As Image = DirectCast(Me.FormImage.Clone(), Image)
            Dim _TempBitmap As New Bitmap(100, 100)
            Dim _TempGraphics As Graphics = Graphics.FromImage(_TempBitmap)
            Dim _Font As New Font("Cambria", 24.0!, FontStyle.Bold, GraphicsUnit.Pixel)
            Dim _FontSizeF As SizeF = _TempGraphics.MeasureString(_TempStr, _Font)
            Dim _FontPointF As New PointF(CSng(_ImageClone.Width - _FontSizeF.Width) / 2.0!, CSng(_ImageClone.Height - _FontSizeF.Height) / 2.0!)
            Dim _FontBrush As New SolidBrush(Color.FromArgb(150, 0, 0, 0))

            Dim _Bitmap As New Bitmap(_ImageClone.Width, _ImageClone.Height)
            Dim _Graphics As Graphics = Graphics.FromImage(_Bitmap)

            Try
                With _Graphics
                    .DrawImage(_ImageClone, New Rectangle(0, 0, Me.Width, Me.Height))

                    .FillRectangle(_FontBrush, _FontPointF.X, _FontPointF.Y - 1.0!, _FontSizeF.Width, _FontSizeF.Height)
                    .DrawString(_TempStr, _Font, Brushes.White, _FontPointF)
                End With

                Me.UpdateVisual(_Bitmap)
            Catch ex As Exception
            Finally
                _TempBitmap.Dispose() : _TempBitmap = Nothing
                _TempGraphics.Dispose() : _TempGraphics = Nothing
                _Font.Dispose() : _Font = Nothing
                _FontPointF = Nothing
                _FontBrush.Dispose() : _FontBrush = Nothing
            End Try
        End Using

    End Sub

    Private Sub BackgroundWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs)
        Me.m_ActionManager.Start()
    End Sub


End Class
